/******************************************************************************
@file:  MenuItems.java
@author Ramesh Anandhan

DESCRIPTION
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
******************************************************************************/

package com.smartc.foodmenu;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.smartc.connection.HttpConnection;
import com.smartc.ui.AccountLogin;
import com.smartc.ui.R;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MenuItems extends Activity {
	protected static final String TAG = "MenuItems";
	private final int PROGRESS_DLG = 0x01;
	private ArrayList<Object> itemList;
	private ItemBean bean;

	private ListView mItemListView;
	private ListViewAdapter adapter;
	private String mSelectedBranch = null;
	private HttpConnection mCityConnection;
	private ResponseHandler mResponse = new ResponseHandler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.fmenu_items);
		Intent intent = getIntent();
		mSelectedBranch = intent.getStringExtra("idBranch");
	}

	public static class ViewHolder {
		TextView MenuTitle;
		TextView MenuList;
	}
	
	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onCreateDialog id" + id);
		switch (id) {	
		case PROGRESS_DLG:
			ProgressDialog mDialog = new ProgressDialog(this);
			mDialog.setMessage("Request In progress....");
			mDialog.setIndeterminate(true);
			mDialog.setCancelable(true);
			return mDialog;
		default:
			return super.onCreateDialog(id, args);
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		String domain = getResources().getString(R.string.domain);
		String query = getResources().getString(R.string.url_food_menu_list);
		String url = domain + query;
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onStart url:" + url);
		mCityConnection = new HttpConnection();
		mCityConnection.setUrl(url);
		mCityConnection.clearParam();
		mCityConnection.AddHandler(mResponse);
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onStart() idBranch:" + mSelectedBranch);
		mCityConnection.AddParam("idBranch", mSelectedBranch);
		mCityConnection.Execute();
		showDialog(PROGRESS_DLG);
	}

	private class ListViewAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		//private Context mContext;
		ArrayList<Object> mItemList;

		ListViewAdapter(Context context, ArrayList<Object> itemList) {
			super();
			//mContext = context;
			mItemList = itemList;
			this.inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return mItemList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.menu_item_list, null);

				holder.MenuTitle = (TextView) convertView
						.findViewById(R.id.fmenu_edit_menu_item);
				holder.MenuList = (TextView) convertView
						.findViewById(R.id.fmenu_edit_menu_list);
				convertView.setTag(holder);
			} else
				holder = (ViewHolder) convertView.getTag();

			ItemBean bean = (ItemBean) itemList.get(position);
			holder.MenuTitle.setText(bean.getMenuTitle());
			holder.MenuList.setText(bean.getMenuList());
			return convertView;
		}
	}

	public class ItemBean {
		String mMenuTitle;
		String mMenuList;

		public String getMenuTitle() {
			return mMenuTitle;
		}

		public void setMenuTitle(String menu) {
			this.mMenuTitle = menu;
		}

		public String getMenuList() {
			return mMenuList;
		}

		public void setMenuList(String list) {
			this.mMenuList = list;
		}
	}

	public void AddObjectToList(String name, String list) {

		bean = new ItemBean();
		bean.setMenuTitle(name);
		bean.setMenuList(list);
		itemList.add(bean);
	}

	public class ResponseHandler extends DefaultHandler {

		//private boolean FoodMenu = false;
		private boolean nmFoodMenu = false;
		private boolean txtFoodMenu = false;

		public ArrayList<String> mMenuTitle = new ArrayList<String>();
		public ArrayList<String> mMenuList = new ArrayList<String>();

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (this.nmFoodMenu) {
				mMenuTitle.add(new String(ch, start, length));
			} else if (this.txtFoodMenu) {
				mMenuList.add(new String(ch, start, length));
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if (localName.equals("FoodMenu")) {
				//this.FoodMenu = false;
				Message myMessage = new Message();
				myMessage.obj = "MENU";
				handler.sendMessage(myMessage);
			} else if (localName.equals("nmFoodMenu")) {
				this.nmFoodMenu = false;
			} else if (localName.equals("txtFoodMenu")) {
				this.txtFoodMenu = false;
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (localName.equals("FoodMenu")) {
				//this.FoodMenu = true;
				mMenuTitle.clear();
				mMenuList.clear();
			} else if (localName.equals("nmFoodMenu")) {
				this.nmFoodMenu = true;
			} else if (localName.equals("txtFoodMenu")) {
				this.txtFoodMenu = true;
			}
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage msg:" + (String) msg.obj);
			String loginmsg = (String) msg.obj;
			if (loginmsg != null && loginmsg.equals("MENU")) {
				// removeDialog(PROGRESS_DLG);
				removeDialog(PROGRESS_DLG);
				
				if(mResponse.mMenuList.size() <= 0){
					TextView header = (TextView) findViewById(R.id.fmenu_header);
					header.setText("No Result");
				}
					
				itemList = new ArrayList<Object>();
				for (int i = 0; i < mResponse.mMenuList.size(); i++) {
					AddObjectToList(mResponse.mMenuTitle.get(i),
							mResponse.mMenuList.get(i));
				}

				mItemListView = (ListView) findViewById(R.id.menu_items);
				adapter = new ListViewAdapter(MenuItems.this
						.getApplicationContext(), itemList);
				mItemListView.setAdapter(adapter);
			}
		}
	};

}
